package com.sy.core.web;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sy.core.ActiveMq.ActiveMqSendMessage;
import com.sy.core.model.SyProduct;
import com.sy.core.model.SyProductAttribute;
import com.sy.core.model.SyProductAttributeValue;
import com.sy.core.model.SyProductSku;
import com.sy.core.util.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.jms.Queue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品模块
 */
@Controller
@RequestMapping("product")
public class ProductCtl extends BaseCtl{
    @Autowired
    private ActiveMqSendMessage activeMqSendMessage;
    @Resource(name="productQueue")
    private Queue productQueue;
    @Resource(name="productDeleteQueue")
    private Queue productDeleteQueue;

    @RequestMapping("index")
    public String index(){
        return "back/product/index";
    }

    @RequestMapping("list")
    @ResponseBody
    public RestResult list(Integer page,Integer limit,SyProduct syProduct){
        RestResult rr = null;
        try {
            PageHelper.startPage(page,limit);
            List<Map<String, Object>> productList = productService.queyList(syProduct);
            PageInfo<Map<String, Object>> info = new PageInfo<>(productList);
            rr=new RestResult("200","获取商品列表数据成功",info);
        } catch (Exception e) {
            logger.error("获取商品列表数据失败");
            rr=new RestResult("201","获取商品列表数据失败");
        }
        return rr;
    }

    /**
     * 新增页面
     * @param mv
     * @return
     */
    @RequestMapping("add")
    public String add(ModelMap mv){
        Map<String,Object> map = new HashMap<>();
        map.put("productTypeParent","0");
        List<Map<String, Object>> firstProductTypeList = productTypeService.queryList(map);//获取所有一级分类的列表信息
        mv.addAttribute("firstProductTypeList",firstProductTypeList);
        return "back/product/productEdit";
    }

    @RequestMapping("update")
    public String update(String pk,ModelMap mv){
        Map<String,Object> map = new HashMap<>();
        map.put("productTypeParent","0");
        List<Map<String, Object>> firstProductTypeList = productTypeService.queryList(map);//获取所有一级分类的列表信息
        mv.addAttribute("firstProductTypeList",firstProductTypeList);

        //查询所有商品
        Map<String, Object> productIfo = productService.queryOne(pk);
        String productPk = (String)productIfo.get("pk");
        mv.addAttribute("mv",productIfo);

        //查询商品的属性
        List list = new ArrayList();
        SyProductAttribute syProductAttribute = new SyProductAttribute();
        SyProductAttributeValue syProductAttributeValue = new SyProductAttributeValue();
        syProductAttribute.setProductPk(productPk);
        List<Map<String, Object>> productAttributeList = productAttributeService.queryList(syProductAttribute);
        for (int i = 0; i <productAttributeList.size() ; i++) {
            Map<String,Object> attrNameAndValue = new HashMap<>();
            syProductAttributeValue.setAttrPk((String)productAttributeList.get(i).get("pk"));
            List<Map<String, Object>> productAttributeValueList = productAttributeValueService.queryList(syProductAttributeValue);
            attrNameAndValue.put("attrName",productAttributeList.get(i).get("attr_name"));
            attrNameAndValue.put("attrValue",productAttributeValueList);
            list.add(attrNameAndValue);
        }
        String jsonStr = JSONObject.toJSONString(list);
        mv.addAttribute("attrNameAndValue",jsonStr);

        //查询该商品下的所有sku
        SyProductSku syProductSku = new SyProductSku();
        syProductSku.setProductPk(productPk);
        List<Map<String,Object>> productSkuList = productSkuService.queryList(syProductSku);
        String productSkuListJson = JSONObject.toJSONString(productSkuList);
        mv.addAttribute("productSkuList",productSkuListJson);
        return "back/product/productEdit";
    }

    /**
     * 下拉select框级联数据
     */
    @RequestMapping("productTypeSecond")
    @ResponseBody
    public RestResult productTypeSecond(String productTypeParent){
        RestResult rr = null;
        Map<String,Object> map = new HashMap<>();
        map.put("productTypeParent",productTypeParent);
        try {
            List<Map<String, Object>> SecondProductTypeList = productTypeService.queryList(map);
            rr = new RestResult("200","获取二级分类数据成功",SecondProductTypeList);
        } catch (Exception e) {
            logger.error("获取二级分类数据失败");
            rr= new RestResult("500","获取二级分类数据失败");
        }
        return rr;
    }

    /**
     * 保存主表和从表数据
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public RestResult saveOrUpdate(String syProduct, String attrNameAndAttrValue, String syProductSku){
        RestResult rr = null;
        try {
            System.out.println(syProduct);
            String pk = productService.save(syProduct, attrNameAndAttrValue, syProductSku);
            //保存或者修改成功以后向mq发送消息
            activeMqSendMessage.sendMessage( productQueue,pk);
            rr = new RestResult("200","保存数据成功",pk);
        } catch (Exception e) {
            logger.error("保存数据失败");
            rr = new RestResult("500","保存数据失败");
        }
        return rr;
    }

    /**
     * 删除功能
     */
    @RequestMapping("del")
    @ResponseBody
    public RestResult del(String pks){
        RestResult rr = null;
        try {
            productService.batchDel(pks);
            rr=new RestResult("200","删除数据成功");
            //删除成功以后向mq发送消息
            activeMqSendMessage.sendMessage( productDeleteQueue,pks);
        } catch (Exception e) {
            logger.error("删除数据失败");
            rr=new RestResult("500","删除数据失败");
        }
        return rr;
    }
}
